package com.example.t410.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.t410.model.PingLunDTO;
import com.example.t410.service.PingLunService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/pinglun")
public class PingLunController {
    @Autowired
    PingLunService pingLunServiceImpl;

    JSONObject jsonObject = new JSONObject();

    @GetMapping("/{id}")
    public JSONObject findById(@PathVariable String id)
    {
        log.info("根据id查询当前讨论下的评论：{}",id);
        jsonObject.clear();
        jsonObject.put("status","200");
        jsonObject.put("data",pingLunServiceImpl.findById(id));
        return jsonObject;
    }
    @PostMapping
    public JSONObject add(@RequestBody PingLunDTO pingLunDTO)
    {
        log.info("根据id查询当前讨论下的评论：{}",pingLunDTO);
        jsonObject.clear();
        jsonObject.put("status","200");
        pingLunServiceImpl.add(pingLunDTO);
        jsonObject.put("data",null);
        return jsonObject;
    }
    @GetMapping("delete/{id}")
    public JSONObject deleteById(@PathVariable String id)
    {
        log.info("根据评论id删除评论：{}",id);
        jsonObject.clear();
        jsonObject.put("status","200");
        pingLunServiceImpl.deleteById(id);
        jsonObject.put("data",null);
        return jsonObject;
    }
}
